import { ref, computed, watch } from 'vue'
import type { Ref } from 'vue'
import { defineStore } from 'pinia'
import u4 from '@/assets/下载 (4).png'

interface iData {
  username?: string,
  password?: string,
  userflog?: boolean,
  useremail?: string,
  photo?: string,
  token?: string,
  phone?: string
}

export const useStore = defineStore('user', () => {

  const userflog: Ref<boolean> = ref(Boolean(localStorage.getItem('flog')) || false)
  const username: Ref<string> = ref(localStorage.getItem('name') || '')
  const useremail: Ref<string> = ref(localStorage.getItem('id') || '')
  const password = ref('')
  const photo: Ref<string> = ref(u4)
  const token: Ref<string | undefined> = ref(localStorage.getItem('token') || '')
  const phone: Ref<string> = ref(localStorage.getItem('phone') || '')


  function changStore(data: iData) {

    username.value = data.username || ''
    password.value = data.password || ''
    userflog.value = data.userflog || false
    useremail.value = data.useremail || ''
    photo.value = data.photo || u4
    phone.value = data.phone || ''


    if (userflog.value == true) {
      token.value = data.token
    } else {
      token.value = ''
    }

  }

  return { username, password, useremail, photo, changStore, token, userflog, phone }
}, {
  persist: true
})
